package co.fitstart.market.web.controller.profile.rest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.demo2do.core.support.Result;
import com.demo2do.core.utils.JsonUtils;

import co.fitstart.entity.market.AchievementStatus;
import co.fitstart.entity.market.MarketAchievement;
import co.fitstart.entity.market.MarketFeedback;
import co.fitstart.entity.user.User;
import co.fitstart.market.service.DataService;
import co.fitstart.market.service.MarketAchievementService;
import co.fitstart.market.web.interceptor.MobileDetect;
import co.fitstart.market.web.interceptor.Router;
import co.fitstart.market.web.interceptor.WechatAuthority;
import co.fitstart.market.web.resolver.Secure;

/**
 * 
 * @author mohanlan
 *
 */
@RestController
@RequestMapping(value = "/profile")
public class ProfileRestController {
    
    @Autowired
    @Qualifier("dataService")
    private DataService<MarketFeedback> feedbackService;
    
    @Autowired
    private MarketAchievementService achievementService;
    
    private static final Logger logger = LoggerFactory.getLogger(ProfileRestController.class);
    
    /**
     * Do feedback
     * 
     * @param user
     * @param feedback
     * @return
     */
    @RequestMapping(value = "/feedbacks", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @Router(targetUrl = "/required-wechat")
    @WechatAuthority
    @MobileDetect
    public @ResponseBody String onFeedback(@Secure User user,
                                           @RequestParam("feedbacks") String feedbacks) {
        
        Result result = new Result();
        
        try {
            
            MarketFeedback marketFeedback = new MarketFeedback(feedbacks, user);
            feedbackService.save(marketFeedback);
            
            result.success().message("反馈成功");
            
        } catch (Exception e) {
            
            logger.error("onFeedback - ", e);
            result.fail().message("反馈失败 ");
        }
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * 
     * @param user
     * @param achievementId
     * @return
     */
    @RequestMapping(value = "/achievements/{achievementId}/receive", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @Router(targetUrl = "/required-wechat")
    @WechatAuthority
    @MobileDetect
    public @ResponseBody String onReceive(@Secure User user,
                                          @PathVariable("achievementId") Long achievementId) {
        
        Result result = new Result();
        
        try {
            
            MarketAchievement achievement = achievementService.load(MarketAchievement.class, achievementId);
            achievementService.update(achievement.changeStatus(AchievementStatus.RECEIVED));
            
            result.success().message("领取成功");
            
        } catch (Exception e) {
            
            logger.error("onReceive - ", e);
            result.fail().message("领取失败 ");
        }
        
        return JsonUtils.toJsonString(result);
    }
}
